package com.mls.fivedistrict.common;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.mls.fivedistrict.enums.ResponseCode;

/**
 * 服务端统一返回的数据类型
 * Created by chenxiuxiang on 2017/12/26.
 */
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServiceResponse<T> {
    private String state;
    private String message;
    private String detail;
    private T data;

    public ServiceResponse() {

    }

    private ServiceResponse(String state) {
        this.state = state;
    }

    private ServiceResponse(String state, String message) {
        this.state = state;
        this.message = message;
    }

    private ServiceResponse(String state, T data) {
        this.state = state;
        this.data = data;
    }

    private ServiceResponse(String state, T data, String detail) {
        this.state = state;
        this.data = data;
        this.detail = detail;
    }

    private ServiceResponse(String state, String message, T data) {
        this.state = state;
        this.message = message;
        this.data = data;
    }

    public static ServiceResponse createErrorResponse() {
        return new ServiceResponse(ResponseCode.ERROR.getCode());
    }


    public static ServiceResponse createErrorResponse(String message) {
        return new ServiceResponse(ResponseCode.ERROR.getCode(), message);
    }

    public static ServiceResponse createErrorResponse(String message, String detail) {
        return new ServiceResponse(ResponseCode.ERROR.getCode(),message, (Object) detail);
    }

    public static ServiceResponse createErrorResponseAndCode(String state, String message) {
        return new ServiceResponse(state, message);
    }

//    public static <T> ServiceResponse<T> createSuccessResponse(String message) {
//        return new ServiceResponse(ResponseCode.SUCCESS.getCode(), message);
//    }

    public static <T> ServiceResponse<T> createSuccessResponse() {
        return new ServiceResponse(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServiceResponse<T> createSuccessResponse(T data) {
        return new ServiceResponse(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> ServiceResponse<T> createSuccessResponse(String message, T data) {
        return new ServiceResponse(ResponseCode.SUCCESS.getCode(), message, data);
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
